<!--
 * @Description:
 * @Autor: 刘诗文
 * @Date: 2020-12-04 17:09:50
 * @LastEditors: 刘诗文
 * @LastEditTime: 2021-02-02 19:35:58
-->
<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :cols="3" :labelWidth="130" :formType="type" :isSubmitBtn="false" />
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { notifyAction, isFormEmpty } from '@/utils';
import { queryOwnerAmountwed, queryPrePayByOwnerNo } from '@/api/customerRepair/customerInfo/ownerVehicleManagement';

export default {
  name: 'customerFinanceInfo',
  mixins: [dictionary],
  props: ['type', 'customerInfo'],
  data() {
    this.selectedData = null;
    return {
      formList: this.createFromList()
    };
  },
  watch: {
    customerInfo(val) {
      this.checkChangeHandle();
    }
  },
  mounted() {},
  methods: {
    createFromList() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '财务信息'
        },
        {
          type: 'INPUT',
          label: '预收款',
          fieldName: 'afterAmount',
          disabled: true,
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '欠款金额',
          fieldName: 'arrearsAmount',
          disabled: true,
          initialValue: ''
        }
      ];
    },
    async checkChangeHandle() {
      if (this.customerInfo && !isFormEmpty(this.customerInfo.ownerNo)) {
        // 获取欠款金额
        this.getArrearsAmount();
        // 获取预收款金额
        this.getPrePayByOwnerNo();
      }
    },
    // 获取欠款金额
    async getArrearsAmount() {
      const res = await queryOwnerAmountwed({ ownerNo: this.customerInfo.ownerNo });
      if (res.resultCode === 200) {
        if (res.errMsg) {
          return notifyAction(res.errMsg, 'error');
        } else {
          if (!res.data) {
            this.formList.find(x => x.fieldName === 'arrearsAmount').initialValue = 0;
          } else {
            this.formList.find(x => x.fieldName === 'arrearsAmount').initialValue = res.data.arrearsAmount;
          }
        }
      }
    },
    // 获取预收款金额
    async getPrePayByOwnerNo() {
      const res = await queryPrePayByOwnerNo({ownerNo: this.customerInfo.ownerNo});
      if (res.resultCode === 200) {
        if (res.errMsg) {
          return notifyAction(res.errMsg, 'error');
        } else {
          if (!res.data) {
            this.formList.find(x => x.fieldName === 'afterAmount').initialValue = 0;
          } else {
            this.formList.find(x => x.fieldName === 'afterAmount').initialValue = res.data;
          }
        }
      }
    }
  }
};
</script>
<style lang="less" scoped></style>
